<?php
$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/river.uswin.us/secure";
include("/home/share/www/SourceSite-utf8/secure/config.inc.php");
include($_SERVER["DOCUMENT_ROOT"]."/config.inc.php");
include($_SERVER["DOCUMENT_ROOT"]."/db.inc.php");

$dbSource = "betRiverUSWinus";
$dbGoal = "betRiverUSWinusUTF8";


$arrTable[] = 'bet_AlertOverBet';
$arrTable[] = 'bet_BetControl';
$arrTable[] = 'bet_BetList';
$arrTable[] = 'bet_BetListContent';
$arrTable[] = 'bet_BetListTmp';
$arrTable[] = 'bet_BetType';
$arrTable[] = 'bet_ChangeWagerStatusLog';
$arrTable[] = 'bet_Commission';
$arrTable[] = 'bet_ExchangeRate';
$arrTable[] = 'bet_Gamble';
$arrTable[] = 'bet_GambleManage';
$arrTable[] = 'bet_GambleScore';
$arrTable[] = 'bet_GambleType';
$arrTable[] = 'bet_LogMarquee';
$arrTable[] = 'bet_LogRecord';
$arrTable[] = 'bet_LoginLog';
$arrTable[] = 'bet_Lotto';
$arrTable[] = 'bet_LottoBetParam';
$arrTable[] = 'bet_LottoLimit';
$arrTable[] = 'bet_LottoOdds';
$arrTable[] = 'bet_LottoType';
$arrTable[] = 'bet_Marquee';
$arrTable[] = 'bet_MarqueeSite';
$arrTable[] = 'bet_Member';
$arrTable[] = 'bet_MemberGroup';
$arrTable[] = 'bet_MemberLimit';
$arrTable[] = 'bet_Odds';
$arrTable[] = 'bet_OldPassword';
$arrTable[] = 'bet_ParlaysNumber';
$arrTable[] = 'bet_ParlaysNumberEven';
$arrTable[] = 'bet_Program';
$arrTable[] = 'bet_RateChangeRecord';
$arrTable[] = 'bet_SelectGambleType';
$arrTable[] = 'bet_Sessions';
$arrTable[] = 'bet_StopRealBet';
$arrTable[] = 'bet_SystemParam';
$arrTable[] = 'bet_TodayGame1';
$arrTable[] = 'bet_TodayGame2';
$arrTable[] = 'bet_TodayGame3';

print "1st\n================================\n\n";
//複製所有資料
$strSQL = "INSERT INTO `".$dbGoal."`.`%s` SELECT * FROM `".$dbSource."`.`%s`";
foreach($arrTable as $strTable){
	print $strTable." => ";
	$strTmpSQL = sprintf($strSQL, $strTable, $strTable);
	if(!mysql_query($strTmpSQL)){
		print "Error! ".mysql_error()."\n";
	}else{
		print "OK! \n";
	}
}

$arrLangTB = array();
//中文轉換成 utf-8
$arrLangTB[] = 'bet_BetType@Type_Value_';
$arrLangTB[] = 'bet_ExchangeRate@Name_';
$arrLangTB[] = 'bet_GambleType@TypeName_';
$arrLangTB[] = 'bet_LogMarquee@Content_';
$arrLangTB[] = 'bet_Lotto@Lotto_';
$arrLangTB[] = 'bet_LottoType@Name_';
$arrLangTB[] = 'bet_Marquee@Content_';
$arrLangTB[] = 'bet_MarqueeSite@Sites_';
$arrLangTB[] = 'bet_MemberGroup@GroupName_';
$arrLangTB[] = 'bet_Program@ProgramName_';

print "\n\n\n2nd\n================================\n\n";
foreach($arrLangTB as $strTable){
	print $strTable." => ";
	$arrT = explode("@", $strTable);
	print "@@".$arrT[1]."@@";

	$strSQL = "select `ID`, `".$arrT[1]."1` as Big5Wd, `".$arrT[1]."3` as GBWd from `".$dbSource."`.`".$arrT[0]."` where 1";
	mysql_query("SET NAMES latin1");
	if(!$RS = mysql_query($strSQL)){
		print "Error! ".mysql_error()."\n";
		continue;
	}else{
		print "OK! \n";
	}

	mysql_query("SET NAMES utf8");
	while($Rows = mysql_fetch_object($RS)){
		$strBig5 = iconv("BIG5", "UTF-8", $Rows -> Big5Wd);
		$strGB = iconv("GB2312", "UTF-8", $Rows -> GBWd);

		$strSQL = "update `".$dbGoal."`.`".$arrT[0]."` SET `".$arrT[1]."1` = '".addslashes($strBig5)."', `".$arrT[1]."3` = '".addslashes($strGB)."' where `ID` = ".$Rows -> ID;
		if(!mysql_query($strSQL)){
			print "SQL Error! ".mysql_error()."\n";
		}
	}
}

$arrLangTB = array();
//BIG5 中文轉換成 utf-8
$arrLangTB[] = 'bet_LogRecord@ActionType@ActionContent';
$arrLangTB[] = 'bet_SystemParam@ParamCName@Illustration';

print "\n\n\n3rd\n================================\n\n";
foreach($arrLangTB as $strTable){
	print $strTable." => ";
	$arrT = explode("@", $strTable);

	mysql_query("SET NAMES latin1");
	$strSQL = "select `ID`, `".$arrT[1]."` as Big5Wd1, `".$arrT[2]."` as Big5Wd2 from `".$dbSource."`.`".$arrT[0]."` where 1";

	if(!$RS = mysql_query($strSQL)){
		print "Error! ".mysql_error()."\n";
		continue;
	}else{
		print "OK! \n";
	}

	mysql_query("SET NAMES utf8");
	while($Rows = mysql_fetch_object($RS)){
		$strBig51 = iconv("BIG5", "UTF-8", $Rows -> Big5Wd1);
		$strBig52 = iconv("BIG5", "UTF-8", $Rows -> Big5Wd2);

		$strSQL = "update `".$dbGoal."`.`".$arrT[0]."` SET `".$arrT[1]."` = '".addslashes($strBig51)."', `".$arrT[2]."` = '".addslashes($strBig52)."' where `ID` = ".$Rows -> ID;
		if(!mysql_query($strSQL)){
			print "SQL Error! ".mysql_error()."\n";
		}
	}
}

$arrLangTB = array();
//欄位裡含有中文的換成 utf-8
//$arrLangTB[] = 'bet_BetListContent@MemAccount';
//$arrLangTB[] = 'bet_LoginLog@Account';
//$arrLangTB[] = 'bet_LogMarquee@Account';
//$arrLangTB[] = 'bet_Member@Account';
$arrLangTB[] = 'bet_Member@Name';

print "\n\n\n4th\n================================\n\n";
foreach($arrLangTB as $strTable){
	print $strTable." => ";
	$arrT = explode("@", $strTable);

	//$strSQL = "select `ID`, `".$arrT[1]."` as Big5Wd, `DefaultLang` from `".$dbSource."`.`".$arrT[0]."` where `".$arrT[1]."` REGEXP '[^[:alnum:]]+' ";
	$strSQL = "select `ID`, `".$arrT[1]."` as Big5Wd, `DefaultLang` from `".$dbSource."`.`".$arrT[0]."` where 1";
	mysql_query("SET NAMES latin1");
	if(!$RS = mysql_query($strSQL)){
		print "Error! ".mysql_error()."\n";
		continue;
	}else{
		print "OK! \n";
	}

	mysql_query("SET NAMES utf8");
	while($Rows = mysql_fetch_object($RS)){
		//if(checkBIG5_GB_ASCII($Rows -> Big5Wd) == "BIG5"){
		if($Rows -> DefaultLang == 1){
			$strBig5 = iconv("BIG5", "UTF-8", $Rows -> Big5Wd);
		}else{
			$strBig5 = iconv("GB2312", "UTF-8", $Rows -> Big5Wd);
		}

		$strSQL = "update `".$dbGoal."`.`".$arrT[0]."` SET `".$arrT[1]."` = '".$strBig5."' where `ID` = ".$Rows -> ID;
		if(!mysql_query($strSQL)){
			print "SQL Error! ".mysql_error()."\n";
		}
	}
}


function checkBIG5_GB_ASCII($strString){

	$intLen = strlen($strString);
	$isChinese = false;
	for($i = 0; $i < $intLen; $i++){
		if(bin2hex(substr($strString, $i, 1)) >= '80'){
			$i++;
			if( bin2hex(substr($strString, $i, 1)) >= '40' && bin2hex(substr($strString, $i, 1)) <= '7e'){
				return "BIG5";
			}
			$isChinese = true;
		}
	}
	return ($isChinese)?"GB":"ASCII";
}
?>